package com.jspgou.cms.entity;

import java.sql.Timestamp;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;

import com.jspgou.cms.entity.base.BaseOrder;

public class Order extends BaseOrder {
	private static final long serialVersionUID = 1L;

	/**
	 * 获得订单商品价格小计
	 * 
	 * @return
	 */
	public Double calSubtotal() {
		Set<OrderItem> items = getItems();
		Double total = 0.0;
		if (items != null) {
			for (OrderItem item : items) {
				if(item.getSeckillprice()!=null){//是否是秒杀产品
					total=total+item.getLimitSubtotal();
				}else{
					total = total+item.getSubtotal();
				}
			}
		}
		return total;
	}
	
	/**
	 * 获得运费
	 * 
	 * @return
	 */
	public Double calFreight() {
//		ShopMemberAddress addr = getDelivery();
//		if (addr == null) {
//			throw new IllegalStateException(
//					"Address is null, cannot calculate price!");
//		}
//		Country country = addr.getCountry();
//		if (country == null) {
//			throw new IllegalStateException(
//					"Country is null, cannot calculate price!");
//		}
		Shipping shipping = getShipping();
		if (shipping == null) {
			throw new IllegalStateException(
					"Shipping is null, cannot calculate price!");
		}
		return shipping.calPrice(getWeightForFreight());
	}

	/**
	 * 计算重量
	 * 
	 * @return
	 */
	public int calWeight() {
		int weight = 0;
		for (OrderItem item : getItems()) {
			weight += item.getProduct().getWeight();
		}
		return weight;
	}

	/**
	 * 获得购物车中商品总重量，免运费的商品不记重量。
	 * 
	 * @return
	 */
	public Double getWeightForFreight() {
		Double weight = 0.0;
		for (OrderItem item : getItems()) {
			weight += item.getWeightForFreight();
		}
		return weight;
	}

	/**
	 * 获得购物车中商品总数量，免运费的商品不计数量。
	 * 
	 * @return
	 */
	public int getCountForFreight() {
		int count = 0;
		for (OrderItem item : getItems()) {
			count += item.getCountForFreigt();
		}
		return count;
	}

	public void addToItems(OrderItem item) {
		Set<OrderItem> items = getItems();
		if (items == null) {
			items = new LinkedHashSet<OrderItem>();
			setItems(items);
		}
		item.setItemOrder(this);
		items.add(item);
	}
	
	public void addToGiftitems(GiftItem item) {
		Set<GiftItem> items = this.getGiftItems();
		if (items == null) {
			items = new LinkedHashSet<GiftItem>();
			this.setGiftItems(items);
		}
		item.setOrder(this);
		items.add(item);
	}

	public void init() {
		Date now = new Timestamp(System.currentTimeMillis());
		if (getCreateTime() == null) {
			setCreateTime(now);
		}
		if (getLastModified() == null) {
			setLastModified(now);
		}
	}

	/**
	 * 获得Enum类型的订单状态
	 * 
	 * @return
	 */
	public OrderStatus getOrderStatus() {
		Integer status = getStatus();
		if (status != null) {
			return OrderStatus.valueOf(status);
		} else {
			return null;
		}
	}

	/* [CONSTRUCTOR MARKER BEGIN] */
	public Order () {
		super();
	}

	/**
	 * Constructor for primary key
	 */
	public Order (java.lang.Long id) {
		super(id);
	}

	/**
	 * Constructor for required fields
	 */
	public Order (
		java.lang.Long id,
		com.jspgou.core.entity.Website website,
		com.jspgou.cms.entity.ShopMember member,
		com.jspgou.cms.entity.Payment payment,
		com.jspgou.cms.entity.Shipping shipping,
		com.jspgou.cms.entity.Shipping shopDirectory,
		com.jspgou.cms.entity.ShopMemberAddress shopMemberAddress,
		java.lang.String code,
		java.lang.String ip,
		java.util.Date createTime,
		java.util.Date lastModified,
		java.lang.Double total,
		java.lang.Integer score,
		java.lang.Double weight,
		java.lang.Integer status) {

		super (
			id,
			website,
			member,
			payment,
			shipping,
			shopDirectory,
			shopMemberAddress,
			code,
			ip,
			createTime,
			lastModified,
			total,
			score,
			weight,
			status);
	}

	/* [CONSTRUCTOR MARKER END] */

	/**
	 * 订单状态
	 * 
	 * @author liufang
	 * 
	 */
	public static enum OrderStatus {
		//等待付款、等待发货、卖家已发货、买家确认收货、退货、卖家同意退货、买家寄出退货、卖家已收退货、退款
		/**
		 * 等待买家付款
		 */
		WAIT_BUYER_PAY(10),
		/**
		 * 买家已付款/等待卖家发货
		 */
		WAIT_SELLER_SEND_GOODS(11),
		/**
		 * 卖家已发货/等待买家确认
		 */
		WAIT_BUYER_CONFIRN_GOODS(12),
		/**
		 * 退款协议等待卖家确认中
		 */
		WAIT_SELLER_AGREE(13),
		/**
		 * 卖家不同意协议，等待买家修改
		 */
		SELLER_REFUSE_BUYER(14),
		/**
		 * 退快协议达成，等待买家退货
		 */
		WAIT_BUYER_RETURN_GOODS(15),
		/**
		 * 等待卖家收货
		 */
		WAIT_SELLER_CONFIRM_GOODS(16),
		/**
		 * 卖家收货
		 */
		REFUND_SUCCESS(17),
		/**
		 * 退款成功
		 */
		RETURN_CLOSED(18),
		
		/**
		 * 完成的订单
		 */
		TRADE_FINISHED(19),
		/**
		 * 交易中途关闭(已结束，为成功完成)
		 */
		TRADE_CLOSE(20);

		private OrderStatus(int value) {
			this.value = value;
		}

		private int value;

		public int getValue() {
			return value;
		}

		public static OrderStatus valueOf(int value) {
			for (OrderStatus s : OrderStatus.values()) {
				if (s.getValue() == value) {
					return s;
				}
			}
			throw new IllegalArgumentException("Connot find value " + value
					+ " in eunu OrderStatus.");
		}
	}

}